package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.common.utils.DateUtils;
import com.cxyd.common.utils.Result;
import com.cxyd.modules.api.report.InoutCount;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.PurchaseDetailDao;
import com.cxyd.modules.wms.dto.OrderReelDTO;
import com.cxyd.modules.wms.dto.PurchaseDTO;
import com.cxyd.modules.wms.dto.PurchaseDetailDTO;
import com.cxyd.modules.wms.entity.OrderReelEntity;
import com.cxyd.modules.wms.entity.PurchaseDetailEntity;
import com.cxyd.modules.wms.service.PurchaseDetailService;
import com.cxyd.modules.wms.service.PurchaseService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 入库明细
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Service
public class PurchaseDetailServiceImpl extends CrudServiceImpl<PurchaseDetailDao, PurchaseDetailEntity, PurchaseDetailDTO> implements PurchaseDetailService {
    private final SysUserService userService;
    private final PurchaseService purchaseService;

    public PurchaseDetailServiceImpl(SysUserService userService, PurchaseService purchaseService) {
        this.userService = userService;
        this.purchaseService = purchaseService;
    }

    @Override
    public PageData<PurchaseDetailDTO> page(Map<String, Object> params) {
        //1.查询主单信息
        final IPage<PurchaseDetailEntity> page = baseDao.selectPage(
                getPage(params, "create_date", false),
                getWrapper(params)
        );
        //2.获取用户信息
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return getPageData(page, PurchaseDetailDTO.class);
    }

    @Override
    public PageData<PurchaseDetailDTO> rfPage(Map<String, Object> params) {
        final PurchaseDTO dto = purchaseService.selectByParams(params);
        if (dto == null) {
            return null;
        }
        final QueryWrapper<PurchaseDetailEntity> rfWrapper = new QueryWrapper<>();
        rfWrapper.eq("purchase_id", dto.getPurchaseId());
        rfWrapper.lt("detail_st", 2);
        rfWrapper.orderBy(true, false, "spu_name");
        final IPage<PurchaseDetailEntity> page = baseDao.selectPage(
                getPage(params, "detail_id", true),
                rfWrapper
        );
        return getPageData(page, PurchaseDetailDTO.class);
    }

    @Override
    public void batchSave(List<PurchaseDetailEntity> entities) {
        if (entities == null) {
            return;
        }
        baseDao.batchSave(entities);
    }

    @Override
    public List<PurchaseDetailDTO> getList(Map<String, Object> params) {
        final List<PurchaseDetailEntity> page = baseDao.selectList(
                getWrapper(params).orderByDesc("create_date")
        );
        //2.获取用户列表
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return ConvertUtils.sourceToTarget(page, PurchaseDetailDTO.class);
    }

    @Override
    public List<InoutCount> getIncount() {
        return baseDao.selectInNum();
    }

    @Override
    public QueryWrapper<PurchaseDetailEntity> getWrapper(Map<String, Object> params) {
        final String purchaseId = (String) params.get("purchaseId");
        final String detailId = (String) params.get("detailId");
        final String spuNo = (String) params.get("spuNo");
        final String spuName = (String) params.get("spuName");
        final String deliverNo = (String) params.get("deliverNo");
        final String lineNo = (String) params.get("lineNo");
        final String detailSt = (String) params.get("detailSt");
        final String contNo = (String) params.get("contNo");
        Date startDate = DateUtils.stringToDate((String) params.get("startDate"), DateUtils.DATE_TIME_PATTERN);
        Date endDate = DateUtils.stringToDate((String) params.get("endDate"), DateUtils.DATE_TIME_PATTERN);


        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(purchaseId), "purchase_id", purchaseId);
        wrapper.eq(StringUtils.isNotBlank(detailId), "detail_id", detailId);
        wrapper.eq(StringUtils.isNotBlank(detailSt), "detail_st", detailSt);
        wrapper.like(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.like(StringUtils.isNotBlank(spuName), "spu_name", spuName);
        wrapper.like(StringUtils.isNotBlank(deliverNo), "deliver_no", deliverNo);
        wrapper.like(StringUtils.isNotBlank(lineNo), "line_no", lineNo);
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.ge(ObjectUtils.isNotEmpty(startDate), "create_date", startDate);
        wrapper.le(ObjectUtils.isNotEmpty(endDate), "create_date", endDate);

        return wrapper;
    }

}